extends Sprite2D

@export var zd : PackedScene

@onready var pt = preload("res://ston/敌人炮台.tscn")

func _ready() -> void:
	
	var _leftpt = pt.instantiate()
	var img = load("res://image/Retina/tankRed_barrel1_outline.png")
	if img:
		_leftpt.texture = img
	_leftpt.DR_open_fire.connect(open_fire)
	_leftpt.position = Vector2(10,0)
	add_child(_leftpt)
	
	var _rightpt = pt.instantiate()
	var rightimg = load("res://image/Retina/tankRed_barrel2_outline.png")
	if rightimg:
		_rightpt.texture = img
	_rightpt.DR_open_fire.connect(open_fire)
	_rightpt.position = Vector2(-10,0)
	add_child(_rightpt)

func _process(delta: float) -> void:
	var guards = get_tree().get_nodes_in_group("play")
	for dr in guards:
		var ptWZ = global_position
		var drWZ = dr.position
		var xdWZ = drWZ - ptWZ
		self.position += xdWZ.normalized() * delta * 40
		self.rotation = xdWZ.angle() - PI/2


func open_fire(wz:Vector2,fx :float):
	print("发现玩家位置",wz)
	var _zd = zd.instantiate()
	_zd._mbName = "paly"
	_zd.SD = 1000
	get_tree().root.add_child(_zd)
	_zd.position = wz
	_zd.rotation = fx + PI
	_zd.fire(Vector2.RIGHT.rotated(fx + PI/2))
